package com.citrix.gs.util;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.citrix.gs.util.beacon.BeaconCI;
import com.citrix.gs.util.beacon.BeaconJobType;
import com.citrix.gs.util.common.CIInfo;
import com.offbytwo.jenkins.model.Job;

public class RepositoryRunner {
    
    //private String repositoryName;
    //private Repository repository;
    
    private CIInfo ciInfo;
    private BeaconCI ci;
    
    public BeaconCI getCi() {
        return ci;
    }

    public void setCi(BeaconCI ci) {
        this.ci = ci;
    }

    public RepositoryRunner(CIInfo ciInfo){
        
        this.ciInfo = ciInfo;
        this.ci = new BeaconCI(ciInfo);
    }
    
    public RepositoryRunner(BeaconCI ci) {
        this.ci = ci;
    }
    
    public void runRepository(String repoName) {
        runRepositories(Arrays.asList(repoName));
    }
    
    public void runRepositories(List<String> repoNameList) {

        List<Job> srcJobList = repoNameList.stream().map(r -> {
            try {
                return ci.getJob(BeaconJobType.Src.name() + "-" + r);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }).collect(Collectors.toList());
        
        ci.runTopJobs(srcJobList);
    }

    public static void main(String[] args) {

    }

}
